#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

typedef struct stack
{
    int top;
    unsigned capacity;
    int *array;
} Stack;

// 创建指定容量的栈. 栈的大小初始化为0
Stack *createStack(unsigned capacity)
{
    Stack *stack = (Stack *)malloc(sizeof(Stack));
    stack->capacity = capacity;
    stack->top = -1;
    // 动态申请了 capacity 个 int 字节的地址空间
    stack->array = (int *)malloc(stack->capacity * sizeof(int));
    return stack;
}

// 栈顶 等于最后一个索引时堆栈已满
int isFull(Stack *stack)
{
    return stack->top == stack->capacity - 1;
}

// 栈顶索引等于-1时，表示空栈
int isEmpty(Stack *stack)
{
    return stack->top == -1;
}

// 进栈
void push(Stack *stack, int item)
{
    if (isFull(stack))
        return;
    stack->array[++stack->top] = item;
    printf("stack pushed>>%c \n", item);
}

// 出栈
int pop(Stack *stack)
{
    if (isEmpty(stack))
        return INT_MIN;
    return stack->array[stack->top--];
}

// 返回栈顶元素
int peek(Stack *stack)
{
    if (isEmpty(stack))
        return INT_MIN;
    return stack->array[stack->top];
}


int main()
{
    Stack *stack = createStack(100);

    // push(stack, 10);
    // push(stack, 20);
    // push(stack, 30);

    printf("-----------seqstack push----------\n");
    char in;
    while ((in = getchar()) != '\n')
    {
        push(stack, in);
    }

    printf("----------seqstack pop-------------\n");
    while (!isEmpty(stack))
    {
        printf("stack pop>> %c\n", pop(stack));
    }

    return 0;
}
